package com.oracle.oa.action;

import java.sql.Timestamp;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.oracle.oa.biz.ManualSignBiz;
import com.oracle.oa.biz.UserInfoBiz;
import com.oracle.oa.entity.Manualsign;
import com.oracle.oa.entity.Userinfo;

public class Statistics extends ActionSupport implements SessionAware {

	Map<String, Object> session;
	
	public String begintime;
	public String endtime;
	public String alltime;
	public String department;
	public String page;

	String[] onelists;
	
	ManualSignBiz msbiz;
	UserInfoBiz ubiz;
	
	@Override
	public String execute() throws Exception {
		
		Long userid=Long.parseLong(session.get("userid").toString());
		Userinfo user=(Userinfo) ubiz.getById(Userinfo.class, userid);
		
		//获取系统当前时间,当天 当月 当前年
		Timestamp now=new Timestamp(System.currentTimeMillis());
		SimpleDateFormat sfm=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sfd=new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat sfy=new SimpleDateFormat("yyyy");
		
		//数字格式转换（百分数）
		NumberFormat num=NumberFormat.getPercentInstance();
		//百分数整数部分最大位数
		num.setMaximumIntegerDigits(5);
		//百分数小数部分不够2位补0
		num.setMinimumFractionDigits(2);
		//百分数小数部分超过2位四舍五入
		num.setMaximumFractionDigits(2);
		
		int week=java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_WEEK);
		String btime="";
		String etime="";
		switch(week){
		case 1:
			btime=sf.format(now.getTime()-6*1000*60*60*24);
			etime=sf.format(now.getTime()+1*1000*60*60*24);
			break;
		case 2:
			btime=sf.format(now);
			etime=sf.format(now.getTime()+7*1000*60*60*24);
			break;
		case 3:
			btime=sf.format(now.getTime()-1*1000*60*60*24);
			etime=sf.format(now.getTime()+6*1000*60*60*24);
			break;
		case 4:
			btime=sf.format(now.getTime()-2*1000*60*60*24);
			etime=sf.format(now.getTime()+5*1000*60*60*24);
			break;
		case 5:
			btime=sf.format(now.getTime()-3*1000*60*60*24);
			etime=sf.format(now.getTime()+4*1000*60*60*24);
			break;
		case 6:
			btime=sf.format(now.getTime()-4*1000*60*60*24);
			etime=sf.format(now.getTime()+3*1000*60*60*24);
			break;
		case 7:
			btime=sf.format(now.getTime()-5*1000*60*60*24);
			etime=sf.format(now.getTime()+2*1000*60*60*24);
			break;
		}
		
		String day=sf.format(now);
		String month=sfd.format(now);
		String year=sfy.format(now);
		
		List lvlist=new ArrayList();
		
		if((begintime==null||begintime.length()==0)&&(endtime==null||endtime.length()==0)&&alltime==null){
			if(department!=null && !("0".equals(department))){
				//如果选择了部门，查询部门中的所有员工
				
				List<Userinfo> duserlist=null;
				
				if(user.getRoleinfo().getRoleid()==1){
					duserlist=ubiz.getByDeptId(Long.parseLong(department));
				}else if(user.getRoleinfo().getRoleid()==2){
					duserlist=ubiz.getByProperty(Userinfo.class, "departid", user.getDepartid());
				}else if(user.getRoleinfo().getRoleid()==3){
					duserlist=new ArrayList<Userinfo>();
					duserlist.add(user);
				}
				
				//查询部门员工的出勤率
				double allday=(sf.parse(day+" 24:00:00").getTime()-sf.parse("1970-01-01").getTime())/1000/60/60/24;
				for(Userinfo u:duserlist){
					if(msbiz.getByIdAndTime("1970-01-01", day+" 24:00:00",u.getUserid())!=null){
					double dayt=msbiz.getByIdAndTime("1970-01-01", day+" 24:00:00",u.getUserid()).size()/2;
					onelists=new String[6];
					onelists[0]=u.getUsername();
					onelists[1]=num.format(msbiz.getChuqin(u, "1970-01-01", day+" 24:00:00","time"))+"错误数据，请选择时间段";
					onelists[2]=msbiz.getLate(u, "1970-01-01", day+" 24:00:00").size()+"错误数据，请选择时间段";
					onelists[3]=msbiz.getEarly(u, "1970-01-01", day+" 24:00:00").size()+"错误数据，请选择时间段";
					onelists[4]=(allday-dayt)+"错误数据，请选择时间段";
					onelists[5]=u.getDepartid().getDepartid()+"";
					
					lvlist.add(onelists);
					}
				}
				
			}else{
				//如果没有选择部门，查询所有员工
				
				List<Userinfo> duserlist=null;
				
				if(user.getRoleinfo().getRoleid()==1){
					duserlist=ubiz.getAll(Userinfo.class);
				}else if(user.getRoleinfo().getRoleid()==2){
					duserlist=ubiz.getByProperty(Userinfo.class, "departid", user.getDepartid());
				}else if(user.getRoleinfo().getRoleid()==3){
					duserlist=new ArrayList<Userinfo>();
					duserlist.add(user);
				}
				
				//查询部门员工的出勤率
				double allday=(sf.parse(day+" 24:00:00").getTime()-sf.parse("1970-01-01").getTime())/1000/60/60/24;
				
				for(Userinfo u:duserlist){
					if(msbiz.getByIdAndTime("1970-01-01", day+" 24:00:00",u.getUserid())!=null){
					double dayt=msbiz.getByIdAndTime("1970-01-01", day+" 24:00:00",u.getUserid()).size()/2;
					onelists=new String[6];
					onelists[0]=u.getUsername();
					onelists[1]=num.format(msbiz.getChuqin(u, "1970-01-01", day+" 24:00:00","time"))+"错误数据，请选择时间段";
					onelists[2]=msbiz.getLate(u, "1970-01-01", day+" 24:00:00").size()+"错误数据，请选择时间段";
					onelists[3]=msbiz.getEarly(u, "1970-01-01", day+" 24:00:00").size()+"错误数据，请选择时间段";
					onelists[4]=(allday-dayt)+"错误数据，请选择时间段";
					onelists[5]=u.getDepartid().getDepartid()+"";
					
					lvlist.add(onelists);
					}
				}
				
			}
			if(page!=null){
				session.put("page", page);
			}
			session.put("lvlist", lvlist);
			//response.sendRedirect("selectMain.jsp");
			
		}else if(((begintime!=null&&begintime.length()!=0)&&(endtime!=null&&endtime.length()!=0)&&alltime==null)||((begintime!=null&&begintime.length()!=0)&&(endtime!=null&&endtime.length()!=0)&&alltime!=null)){
			//选择了起始和结束时间
			
			if(sf.parse(endtime).getTime()<sf.parse(begintime).getTime()){
				String temp=begintime;
				begintime=endtime;
				endtime=temp;
			}
			
			if(department!=null && !("0".equals(department))){
				//如果选择了部门，查询部门中的所有员工
				
				List<Userinfo> duserlist=null;
				
				if(user.getRoleinfo().getRoleid()==1){
					duserlist=ubiz.getByDeptId(Long.parseLong(department));
				}else if(user.getRoleinfo().getRoleid()==2){
					duserlist=ubiz.getByProperty(Userinfo.class, "departid", user.getDepartid());
				}else if(user.getRoleinfo().getRoleid()==3){
					duserlist=new ArrayList<Userinfo>();
					duserlist.add(user);
				}
				
				//查询部门员工的出勤率
				double allday=(sf.parse(endtime.toString()).getTime()-sf.parse(begintime.toString()).getTime())/1000/60/60/24;
				for(Userinfo u:duserlist){
					if(msbiz.getByIdAndTime(begintime, endtime,u.getUserid())!=null){
					double dayt=msbiz.getByIdAndTime(begintime, endtime,u.getUserid()).size()/2;
					onelists=new String[6];
					//员工姓名
					onelists[0]=u.getUsername();
					//出勤率（%）
					onelists[1]=num.format(msbiz.getChuqin(u, begintime.toString(), endtime.toString(),"time"))+"";
					//迟到天数
					onelists[2]=msbiz.getLate(u, begintime.toString(), endtime.toString()).size()+"";
					//早退天数
					onelists[3]=msbiz.getEarly(u, begintime.toString(), endtime.toString()).size()+"";
					//旷工天数
					onelists[4]=(allday-dayt)+"";
					//所属部门
					onelists[5]=u.getDepartid().getDepartid()+"";
					lvlist.add(onelists);
					}
				}
				
			}else{
				//如果没有选择部门，查询所有员工
				
				List<Userinfo> duserlist=null;
				
				if(user.getRoleinfo().getRoleid()==1){
					duserlist=ubiz.getAll(Userinfo.class);
				}else if(user.getRoleinfo().getRoleid()==2){
					duserlist=ubiz.getByProperty(Userinfo.class, "departid", user.getDepartid());
				}else if(user.getRoleinfo().getRoleid()==3){
					duserlist=new ArrayList<Userinfo>();
					duserlist.add(user);
				}
				
				//查询部门员工的出勤率
				double allday=(sf.parse(endtime).getTime()-sf.parse(begintime).getTime())/1000/60/60/24;
				
				for(Userinfo u:duserlist){
					if(msbiz.getByIdAndTime(begintime, endtime,u.getUserid())!=null){
					double dayt=msbiz.getByIdAndTime(begintime, endtime,u.getUserid()).size()/2;
					onelists=new String[6];
					onelists[0]=u.getUsername();
					onelists[1]=num.format(msbiz.getChuqin(u, begintime.toString(), endtime.toString(),"time"))+"";
					onelists[2]=msbiz.getLate(u, begintime.toString(), endtime.toString()).size()+"";
					onelists[3]=msbiz.getEarly(u, begintime.toString(), endtime.toString()).size()+"";
					onelists[4]=(allday-dayt)+"";
					onelists[5]=u.getDepartid().getDepartid()+"";
					
					lvlist.add(onelists);
					}
				}
				
			}
			if(page!=null){
				session.put("page", page);
			}
			session.put("lvlist", lvlist);
			//response.sendRedirect("selectMain.jsp");
			
		}else if((begintime==null||begintime.length()==0)&&(endtime==null||endtime.length()==0)&&alltime!=null){
			
			List<Manualsign> slmslist=null;
			if("day".equals(alltime)){
				
				if(department!=null && !("0".equals(department))){
					//如果选择了部门，查询部门中的所有员工
					
					List<Userinfo> duserlist=null;
					
					if(user.getRoleinfo().getRoleid()==1){
						duserlist=ubiz.getByDeptId(Long.parseLong(department));
					}else if(user.getRoleinfo().getRoleid()==2){
						duserlist=ubiz.getByProperty(Userinfo.class, "departid", user.getDepartid());
					}else if(user.getRoleinfo().getRoleid()==3){
						duserlist=new ArrayList<Userinfo>();
						duserlist.add(user);
					}
					
					//查询部门员工的出勤率
					double allday=(sfm.parse(day+" 24:00:00").getTime()-sfm.parse(day+" 00:00:00").getTime())/1000/60/60/24;
					for(Userinfo u:duserlist){
						if(msbiz.getByIdAndTime(day+" 00:00:00", day+" 24:00:00",u.getUserid())!=null){
						double dayt=msbiz.getByIdAndTime(day+" 00:00:00", day+" 24:00:00",u.getUserid()).size()/2;
						onelists=new String[6];
						onelists[0]=u.getUsername();
						onelists[1]=num.format(msbiz.getChuqin(u, day+" 00:00:00", day+" 24:00:00","day"))+"";
						onelists[2]=msbiz.getLate(u, day+" 00:00:00", day+" 24:00:00").size()+"";
						onelists[3]=msbiz.getEarly(u, day+" 00:00:00", day+" 24:00:00").size()+"";
						onelists[4]=(allday-dayt)+"";
						onelists[5]=u.getDepartid().getDepartid()+"";
						
						lvlist.add(onelists);
						}
					}
					
				}else{
					//如果没有选择部门，查询所有员工
					
					List<Userinfo> duserlist=null;
					
					if(user.getRoleinfo().getRoleid()==1){
						duserlist=ubiz.getAll(Userinfo.class);
					}else if(user.getRoleinfo().getRoleid()==2){
						duserlist=ubiz.getByProperty(Userinfo.class, "departid", user.getDepartid());
					}else if(user.getRoleinfo().getRoleid()==3){
						duserlist=new ArrayList<Userinfo>();
						duserlist.add(user);
					}
					//查询部门员工的出勤率
					double allday=(sfm.parse(day+" 24:00:00").getTime()-sfm.parse(day+" 00:00:00").getTime())/1000/60/60/24;
					for(Userinfo u:duserlist){
						if(msbiz.getByIdAndTime(day+" 00:00:00", day+" 24:00:00",u.getUserid())!=null){
						double dayt=msbiz.getByIdAndTime(day+" 00:00:00", day+" 24:00:00",u.getUserid()).size()/2;
						onelists=new String[6];
						onelists[0]=u.getUsername();
						onelists[1]=num.format(msbiz.getChuqin(u, day+" 00:00:00".toString(), day+" 24:00:00".toString(),"day"))+"";
						onelists[2]=msbiz.getLate(u, day+" 00:00:00".toString(), day+" 24:00:00".toString()).size()+"";
						onelists[3]=msbiz.getEarly(u, day+" 00:00:00".toString(), day+" 24:00:00".toString()).size()+"";
						onelists[4]=(allday-dayt)+"";
						onelists[5]=u.getDepartid().getDepartid()+"";
						
						lvlist.add(onelists);
						}
					}
					
				}
				
			}else if("mouth".equals(alltime)){
				if(department!=null && !("0".equals(department))){
					//如果选择了部门，查询部门中的所有员工
					List<Userinfo> duserlist=null;
					
					if(user.getRoleinfo().getRoleid()==1){
						duserlist=ubiz.getByDeptId(Long.parseLong(department));
					}else if(user.getRoleinfo().getRoleid()==2){
						duserlist=ubiz.getByProperty(Userinfo.class, "departid", user.getDepartid());
					}else if(user.getRoleinfo().getRoleid()==3){
						duserlist=new ArrayList<Userinfo>();
						duserlist.add(user);
					}
					//查询部门员工的出勤率
					double allday=(sf.parse(month+"-31").getTime()-sf.parse(month+"-01").getTime())/1000/60/60/24;
					
					for(Userinfo u:duserlist){
						if(msbiz.getByIdAndTime(month+"-01", month+"-31",u.getUserid())!=null){
						double dayt=msbiz.getByIdAndTime(month+"-01", month+"-31",u.getUserid()).size()/2;
						onelists=new String[6];
						onelists[0]=u.getUsername();
						onelists[1]=num.format(msbiz.getChuqin(u, month+"-01", month+"-31","mouth"))+"";
						onelists[2]=msbiz.getLate(u, month+"-01", month+"-31").size()+"";
						onelists[3]=msbiz.getEarly(u, month+"-01", month+"-31").size()+"";
						onelists[4]=(allday-dayt-8)+"";
						onelists[5]=u.getDepartid().getDepartid()+"";
						
						lvlist.add(onelists);
						}
					}
					
				}else{
					//如果没有选择部门，查询所有员工
					List<Userinfo> duserlist=null;
					
					if(user.getRoleinfo().getRoleid()==1){
						duserlist=ubiz.getAll(Userinfo.class);
					}else if(user.getRoleinfo().getRoleid()==2){
						duserlist=ubiz.getByProperty(Userinfo.class, "departid", user.getDepartid());
					}else if(user.getRoleinfo().getRoleid()==3){
						duserlist=new ArrayList<Userinfo>();
						duserlist.add(user);
					}
					//查询部门员工的出勤率
					double allday=(sf.parse(month+"-31").getTime()-sf.parse(month+"-01").getTime())/1000/60/60/24;
					for(Userinfo u:duserlist){
						if(msbiz.getByIdAndTime(month+"-01", month+"-31",u.getUserid())!=null){
						double dayt=msbiz.getByIdAndTime(month+"-01", month+"-31",u.getUserid()).size()/2;
						onelists=new String[6];
						onelists[0]=u.getUsername();
						onelists[1]=num.format(msbiz.getChuqin(u, month+"-01", month+"-31","mouth"))+"";
						onelists[2]=msbiz.getLate(u, month+"-01", month+"-31").size()+"";
						onelists[3]=msbiz.getEarly(u, month+"-01", month+"-31").size()+"";
						onelists[4]=(allday-dayt-8)+"";
						onelists[5]=u.getDepartid().getDepartid()+"";
						
						lvlist.add(onelists);
						}
					}
					
				}
				
			}else if("year".equals(alltime)){
				if(department!=null && !("0".equals(department))){
					//如果选择了部门，查询部门中的所有员工
					List<Userinfo> duserlist=null;
					
					if(user.getRoleinfo().getRoleid()==1){
						duserlist=ubiz.getByDeptId(Long.parseLong(department));
					}else if(user.getRoleinfo().getRoleid()==2){
						duserlist=ubiz.getByProperty(Userinfo.class, "departid", user.getDepartid());
					}else if(user.getRoleinfo().getRoleid()==3){
						duserlist=new ArrayList<Userinfo>();
						duserlist.add(user);
					}
					//查询部门员工的出勤率
					double allday=(sf.parse(year+"-12-31").getTime()-sf.parse(year+"-01-01").getTime())/1000/60/60/24;
					for(Userinfo u:duserlist){
						if(msbiz.getByIdAndTime(year+"-01-01", year+"-12-31",u.getUserid())!=null){
						double dayt=msbiz.getByIdAndTime(year+"-01-01", year+"-12-31",u.getUserid()).size()/2;
						onelists=new String[6];
						onelists[0]=u.getUsername();
						onelists[1]=num.format(msbiz.getChuqin(u, year+"-01-01", year+"-12-31","year"))+"";
						onelists[2]=msbiz.getLate(u, year+"-01-01", year+"-12-31").size()+"";
						onelists[3]=msbiz.getEarly(u, year+"-01-01", year+"-12-31").size()+"";
						onelists[4]=(allday-dayt-96)+"";
						onelists[5]=u.getDepartid().getDepartid()+"";
						
						lvlist.add(onelists);
						}
					}
					
				}else{
					//如果没有选择部门，查询所有员工
					List<Userinfo> duserlist=null;
					
					if(user.getRoleinfo().getRoleid()==1){
						duserlist=ubiz.getAll(Userinfo.class);
					}else if(user.getRoleinfo().getRoleid()==2){
						duserlist=ubiz.getByProperty(Userinfo.class, "departid", user.getDepartid());
					}else if(user.getRoleinfo().getRoleid()==3){
						duserlist=new ArrayList<Userinfo>();
						duserlist.add(user);
					}
					//查询部门员工的出勤率
					double allday=(sf.parse(year+"-12-31").getTime()-sf.parse(year+"-01-01").getTime())/1000/60/60/24;

					for(Userinfo u:duserlist){
						if(msbiz.getByIdAndTime(year+"-01-01", year+"-12-31",u.getUserid())!=null){
						double dayt=msbiz.getByIdAndTime(year+"-01-01", year+"-12-31",u.getUserid()).size()/2;
						onelists=new String[6];
						onelists[0]=u.getUsername();
						onelists[1]=num.format(msbiz.getChuqin(u, year+"-01-01", year+"-12-31","year"))+"";
						onelists[2]=msbiz.getLate(u, year+"-01-01", year+"-12-31").size()+"";
						onelists[3]=msbiz.getEarly(u, year+"-01-01", year+"-12-31").size()+"";
						onelists[4]=(allday-dayt-96)+"";
						onelists[5]=u.getDepartid().getDepartid()+"";
						
						lvlist.add(onelists);
						}
					}
					
				}
				
			}else if("week".equals(alltime)){

				if(department!=null && !("0".equals(department))){
					//如果选择了部门，查询部门中的所有员工
					List<Userinfo> duserlist=null;
					
					if(user.getRoleinfo().getRoleid()==1){
						duserlist=ubiz.getByDeptId(Long.parseLong(department));
					}else if(user.getRoleinfo().getRoleid()==2){
						duserlist=ubiz.getByProperty(Userinfo.class, "departid", user.getDepartid());
					}else if(user.getRoleinfo().getRoleid()==3){
						duserlist=new ArrayList<Userinfo>();
						duserlist.add(user);
					}
					//查询部门员工的出勤率
					double allday=(sf.parse(btime).getTime()-sf.parse(etime).getTime())/1000/60/60/24;
					for(Userinfo u:duserlist){
						if(msbiz.getByIdAndTime(btime, etime,u.getUserid())!=null){
						double dayt=msbiz.getByIdAndTime(btime, etime,u.getUserid()).size()/2;
						onelists=new String[6];
						onelists[0]=u.getUsername();
						onelists[1]=num.format(msbiz.getChuqin(u, btime, etime,"week"))+"";
						onelists[2]=msbiz.getLate(u, btime, etime).size()+"";
						onelists[3]=msbiz.getEarly(u, btime, etime).size()+"";
						onelists[4]=(allday-dayt-2)+"";
						onelists[5]=u.getDepartid().getDepartid()+"";
						
						lvlist.add(onelists);
						}
					}
					
				}else{
					//如果没有选择部门，查询所有员工
					List<Userinfo> duserlist=null;
					
					if(user.getRoleinfo().getRoleid()==1){
						duserlist=ubiz.getAll(Userinfo.class);
					}else if(user.getRoleinfo().getRoleid()==2){
						duserlist=ubiz.getByProperty(Userinfo.class, "departid", user.getDepartid());
					}else if(user.getRoleinfo().getRoleid()==3){
						duserlist=new ArrayList<Userinfo>();
						duserlist.add(user);
					}
					//查询部门员工的出勤率
					double allday=(sf.parse(etime).getTime()-sf.parse(btime).getTime())/1000/60/60/24;

					for(Userinfo u:duserlist){
						if(msbiz.getByIdAndTime(btime, etime,u.getUserid())!=null){
						double dayt=msbiz.getByIdAndTime(btime, etime,u.getUserid()).size()/2;
						onelists=new String[6];
						onelists[0]=u.getUsername();
						onelists[1]=num.format(msbiz.getChuqin(u, btime, etime,"week"))+"";
						onelists[2]=msbiz.getLate(u, btime, etime).size()+"";
						onelists[3]=msbiz.getEarly(u, btime, etime).size()+"";
						onelists[4]=(allday-dayt-2)+"";
						onelists[5]=u.getDepartid().getDepartid()+"";
						
						lvlist.add(onelists);
						}
					}
					
				}
				
			
			}
			if(page!=null){
				session.put("page", page);
			}
			session.put("lvlist", lvlist);
			//response.sendRedirect("selectMain.jsp");
		}
		int type=1;
		session.put("type", type);
		return SUCCESS;
	}

	public void setSession(Map<String, Object> arg0) {
		this.session=arg0;
	}

	public String getBegintime() {
		return begintime;
	}

	public void setBegintime(String begintime) {
		this.begintime = begintime;
	}

	public String getEndtime() {
		return endtime;
	}

	public void setEndtime(String endtime) {
		this.endtime = endtime;
	}

	public String getAlltime() {
		return alltime;
	}

	public void setAlltime(String alltime) {
		this.alltime = alltime;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public ManualSignBiz getMsbiz() {
		return msbiz;
	}

	public void setMsbiz(ManualSignBiz msbiz) {
		this.msbiz = msbiz;
	}

	public UserInfoBiz getUbiz() {
		return ubiz;
	}

	public void setUbiz(UserInfoBiz ubiz) {
		this.ubiz = ubiz;
	}

	
}
